package a06_栈与队列;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * <p>
     * a04_删除字符串中的所有相邻重复项
 * </p>
 *
 * @author flyduck
 * @since 2024-08-13
 */
public class a05_删除字符串中的所有相邻重复项 {

    public static void main(String[] args) {
        System.out.println(removeDuplicates("abbaca"));
    }

    //abbaca
    public static String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();

        char[] chars = s.toCharArray();
        for (char c : chars) {
            if(!stack.isEmpty() && stack.peek().equals(c)){
                stack.pop();
            }else {
                stack.add(c);
            }
        }
        char[] chars1 = new char[stack.size()];
        for (int i = stack.size() - 1; i >= 0; i--) {
            chars1[i] = stack.pop();
        }
        return new String(chars1);
    }
}
